/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CompetenceView.java
 *
 * Created on 27.05.2013, 19:24:29
 */
package db21;

import db21.entities.Competences;
import db21.listeners.ClickOnlyListener;
import java.awt.event.MouseEvent;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
 *
 * @author Mamontov
 */
public class CompetenceView extends ApplicationPanel {

    /** Creates new form CompetenceView */
    public CompetenceView() {
        initComponents();
        JButton btns[] = {btnToResources, btnToThemes,  btnToMain };
        ApplicationMode modes[] = { 
            ApplicationMode.EducationalResources,
            ApplicationMode.Themes,
            ApplicationMode.MainForm
        };
        for(int i = 0; i < btns.length; i++) {
            this.bindOnClickListenerTogglingToMode(btns[i], modes[i]);
        }
        bindSave(btnSave);
        
        DefaultListModel m = new DefaultListModel();
        lstAllCompetences.setModel(m);
        
        
        btnAdd.addMouseListener(new ClickOnlyListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                String individualName = CompetenceView.this.txtName.getText();
                if (individualName.isEmpty() == false) {                    
                    DefaultListModel k = (DefaultListModel)(CompetenceView.this.lstAllCompetences.getModel());
                    boolean result = m_model.resource(Competences.class).makeIndividual(individualName);
                    if (result) {
                        k.addElement(individualName);
                    } else {
                         JOptionPane.showMessageDialog(CompetenceView.this, "Individual already exists");
                    }
                }
            }
        });
    }

    @Override
    public void fireModelUpdate() {
        this.m_model
            .resource(Competences.class)
            .populateListWithEntities(lstAllCompetences);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        lstAllCompetences = new javax.swing.JList();
        btnAdd = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnToMain = new javax.swing.JButton();
        btnToResources = new javax.swing.JButton();
        btnToThemes = new javax.swing.JButton();
        txtName = new javax.swing.JTextField();
        lblName = new javax.swing.JLabel();
        btnSave = new javax.swing.JButton();

        lstAllCompetences.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lstAllCompetences.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstAllCompetencesValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(lstAllCompetences);

        btnAdd.setText("Добавить");

        btnDelete.setText("Удалить");

        btnToMain.setText("К главному окну");

        btnToResources.setText("К ресурсам");

        btnToThemes.setText("К темам");

        lblName.setText("Название");

        btnSave.setText("Сохранить");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(lblName)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtName, javax.swing.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnAdd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnToThemes, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(btnSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnToResources, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(btnDelete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnToMain, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE))))
                .addGap(24, 24, 24))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(24, 24, 24)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 351, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(25, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(295, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnAdd)
                        .addComponent(btnSave))
                    .addComponent(btnDelete))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnToMain)
                    .addComponent(btnToResources)
                    .addComponent(btnToThemes))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(129, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void lstAllCompetencesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstAllCompetencesValueChanged
        DefaultListModel k = (DefaultListModel)(lstAllCompetences.getModel());
        Integer index = lstAllCompetences.getSelectedIndex();                
        boolean adjust = lstAllCompetences.getValueIsAdjusting();
        if (index != -1 && !adjust) {
            String text = k.get(index).toString();
            txtName.setText(text);
       }
    }//GEN-LAST:event_lstAllCompetencesValueChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnToMain;
    private javax.swing.JButton btnToResources;
    private javax.swing.JButton btnToThemes;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblName;
    private javax.swing.JList lstAllCompetences;
    private javax.swing.JTextField txtName;
    // End of variables declaration//GEN-END:variables
}
